/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NerSemi.java
 *
 * Created on Aug 29, 2011, 4:28:52 AM
 */
package crfsvm;

import crfsvm.crf.een_phuong.CopyFile;
import crfsvm.crf.een_phuong.TaggedDocument;
import crfsvm.util.Document;
import crfsvm.util.FileUtils;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 *
 * @author banhbaochay
 */
public class NerSemi extends javax.swing.JFrame implements PropertyChangeListener {

    static Logger logger = Logger.getLogger(NerSemi.class);

    static {
        DOMConfigurator.configure("log-config.xml");
    }

    /** Creates new form NerSemi */
    public NerSemi() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        trainTxf = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        testTxf = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        runButton = new javax.swing.JButton();
        advButton = new javax.swing.JToggleButton();
        settingPanel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        bTxf = new javax.swing.JTextField();
        sTxf = new javax.swing.JTextField();
        thresholdTxf = new javax.swing.JTextField();
        bagSizeTxf = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        splitTestTxf = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Using semi for NER problem");

        mainPanel.setName("mainPanel"); // NOI18N

        jLabel1.setText("Train file:");
        jLabel1.setName("jLabel1"); // NOI18N

        trainTxf.setName("trainTxf"); // NOI18N

        jLabel2.setText("Test file:");
        jLabel2.setName("jLabel2"); // NOI18N

        testTxf.setName("testTxf"); // NOI18N
        testTxf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testTxfActionPerformed(evt);
            }
        });

        jPanel2.setName("jPanel2"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        runButton.setText("Run");
        runButton.setName("runButton"); // NOI18N
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });
        jPanel1.add(runButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 6, 89, -1));

        advButton.setText("Advanced");
        advButton.setName("advButton"); // NOI18N
        advButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                advButtonActionPerformed(evt);
            }
        });
        jPanel1.add(advButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(204, 6, -1, -1));

        jPanel2.add(jPanel1);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(testTxf, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                    .addComponent(trainTxf, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE))
                .addGap(72, 72, 72))
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(trainTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(testTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        settingPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Settings", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
        settingPanel.setName("settingPanel"); // NOI18N
        settingPanel.setVisible(false);

        jPanel3.setName("jPanel3"); // NOI18N

        jLabel3.setText("Baggings number B:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Batch size S:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Threshold:");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("Bag 's size:");
        jLabel6.setName("jLabel6"); // NOI18N

        bTxf.setText("5");
        bTxf.setName("bTxf"); // NOI18N

        sTxf.setText("50");
        sTxf.setName("sTxf"); // NOI18N

        thresholdTxf.setText("0.227");
        thresholdTxf.setName("thresholdTxf"); // NOI18N

        bagSizeTxf.setText("80");
        bagSizeTxf.setName("bagSizeTxf"); // NOI18N

        jLabel7.setText("Test split:");
        jLabel7.setName("jLabel7"); // NOI18N

        splitTestTxf.setText("5");
        splitTestTxf.setName("splitTestTxf"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(thresholdTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(sTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(bTxf, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bagSizeTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(splitTestTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel3, jLabel4, jLabel5, jLabel6});

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bTxf, bagSizeTxf, sTxf, splitTestTxf, thresholdTxf});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(bTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(bagSizeTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(sTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(splitTestTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(thresholdTxf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        settingPanel.add(jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(settingPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
                    .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(settingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void advButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_advButtonActionPerformed
// TODO add your handling code here:
    boolean isSelected = advButton.isSelected();
    settingPanel.setVisible(isSelected);
    pack();
}//GEN-LAST:event_advButtonActionPerformed

private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed
// TODO add your handling code here:
    progressMonitor = new ProgressMonitor(NerSemi.this, "Running semi-supervised", "", 0, 100);
    progressMonitor.setProgress(0);
    task = new Task();
    task.addPropertyChangeListener(this);
    task.execute();
    runButton.setEnabled(false);
}//GEN-LAST:event_runButtonActionPerformed

private void testTxfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testTxfActionPerformed
    runButtonActionPerformed(evt);
}//GEN-LAST:event_testTxfActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NerSemi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NerSemi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NerSemi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NerSemi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NerSemi().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton advButton;
    private javax.swing.JTextField bTxf;
    private javax.swing.JTextField bagSizeTxf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton runButton;
    private javax.swing.JTextField sTxf;
    private javax.swing.JPanel settingPanel;
    private javax.swing.JTextField splitTestTxf;
    private javax.swing.JTextField testTxf;
    private javax.swing.JTextField thresholdTxf;
    private javax.swing.JTextField trainTxf;
    // End of variables declaration//GEN-END:variables
    private Task task;
    private ProgressMonitor progressMonitor;

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (progressMonitor.isCanceled()) {
            task.cancel(true);
        } else if (evt.getPropertyName().equals("progress")) {
            int progress = (Integer) evt.getNewValue();
            progressMonitor.setProgress(progress);
        }// end if progress property is changed
    }// end propertyChange

    /**
     * Thuc hien tien trinh
     */
    class Task extends SwingWorker<Void, Void> {

        @Override
        /**
         * Thuc hien semi-supervised
         */
        protected Void doInBackground() throws Exception {
            int progress = 0;
            setProgress(0);
            String message = null;
            try {
                Thread.sleep(1000);
                setProgress(1);
                message = String.format("Train va predict voi tap goc\nCompleted %d%%\n", 1);
                progressMonitor.setNote(message);
            } catch (Exception e) {}
            
            // Cac thong so lay tu Setting
            String mainTrain = trainTxf.getText();
            String mainTest = testTxf.getText();
            Main.B = Integer.parseInt(bTxf.getText());
            Main.thresholdH = Double.parseDouble(thresholdTxf.getText());
            Main.bagSize = Integer.parseInt(bagSizeTxf.getText());
            Main.S = Integer.parseInt(sTxf.getText());
            int splitTest = Integer.parseInt(splitTestTxf.getText());
            int totalLoop = Main.B * splitTest + splitTest + 1;


            // Cac thong so danh rieng cho chuong trinh
            Main m = new Main();
            String mainTrainCopied = "tmp/train.txt";
            String mainTestCopied = "tmp/test.txt";
            // File feature goc tao ra tu file train
            String mainTrainFeature = mainTrainCopied + ".feature";
            String bagTrainCopied = "tmp/bagTrain.txt";
            String bagTestCopied = "tmp/bagTest.txt";

            /*
             * Tao model va file feature dau tien. File feature: oriTrain + .feature
             */
            logger.info("Tao model va file feature dau tien, dong thoi tinh toan P-R-F");
            CopyFile.copyfile(mainTrain, mainTrainCopied);
            CopyFile.copyfile(mainTest, mainTestCopied);
            Crf.calcFScore(mainTrainCopied, mainTestCopied);
            
            // Thay doi progress
            progress += 100 / totalLoop;
            setProgress(Math.min(progress, 100));
            message = String.format("Tao train set");
            progressMonitor.setNote(message);

            /*
             * Tao TrainSet: tmp/TrainSet
             */
            logger.info("Tao TrainSet");
            Document tmpDoc = new Document(mainTrain);
            m.createTrainSet(tmpDoc, Main.B, Main.bagSize);

            message = String.format("Tao test set");
            progressMonitor.setNote(message);
            
            /*
             * Tao TestSet: tmp/TestSet
             */
            logger.info("Tao TestSet");
            tmpDoc = new Document(mainTest);
            m.createTestSet(tmpDoc, splitTest);
            tmpDoc = null;
            /*
             * Lap semi
             */
            logger.info("Bat dau lap semi");
            File testSetDir = new File("tmp/TestSet");
            for (File subTest : testSetDir.listFiles()) {
                /*
                 * Bat dau thuc hien voi 1 file test trong TestSet
                 */
                FileUtils.removeTag(subTest);
                // Copy file test trong TestSet ra thu muc tmp
                try {
                    CopyFile.copyfile(subTest.getAbsolutePath(), bagTestCopied);
                } catch (Exception e) {
                    logger.debug(e.getMessage());
                }// end try
                String subTestTagged = bagTestCopied + ".tagged";
                logger.info("Thuc hien voi 1 file test");
                // Chuan bi
                logger.info("Tach tu file test");

                // Tao file tach tu doi voi file test
                Crf.runVnTagger(bagTestCopied, subTestTagged);

                TaggedDocument subTestDoc = new TaggedDocument(subTestTagged);
                Map<String, Map<String, Integer>> countMap = new HashMap<String, Map<String, Integer>>();

                // Bat dau vong lap CRF
                logger.info("Bat dau boostrapping");
                File trainSetDir = new File("tmp/TrainSet");
                for (File trainBagFile : trainSetDir.listFiles()) {
                    /*
                     * Lap voi tung bag
                     */
                    try {
                        CopyFile.copyfile(trainBagFile.getAbsolutePath(), bagTrainCopied);
                    } catch (Exception e) {
                        logger.debug(e.getMessage());
                    }// end try
                    // train + predict, ket qua predict nam trong file bagTestCopied + wseg
                    logger.info("Chay CRF voi file " + bagTrainCopied);
                    Crf.runCrf(bagTrainCopied, bagTestCopied);
                    m.countAppear(countMap, bagTestCopied + ".wseg");
                    
                    // Thay doi progress
                    progress += 100 / totalLoop;
                    setProgress(Math.min(progress, 100));
                }// end foreach CRF

                // Lay ra S phan tu co entropy nho nhat lon hon nguong
                List<String[]> sList = m.calcAndFindS(countMap);

                // Them dac trung cua S tu duoc gan nhan nay vao file dac trung ban dau.
                // File dac trung ban dau co dang: mainTrain + .feature
                logger.info("Them dac trung moi vao file dac trung goc");
                m.processAfterPredict(sList, subTestDoc, mainTrainFeature);

                //Tao model moi tu file dac trung moi duoc them + tinh toan P-R-F
                logger.info("Tao model moi tu file dac trung moi them");
                Crf.calcFScore(mainTrainFeature, mainTestCopied);
                
                // Thay doi progress
                progress += 100 / totalLoop;
                setProgress(Math.min(progress, 100));

            }// end foreach testFile
            // Ket thuc lap semi

            return null;
        }

        @Override
        protected void done() {
            progressMonitor.close();
            JOptionPane.showMessageDialog(null, "Process done");
            runButton.setEnabled(true);
        }
    }// end Task class
}
